package collection.myset;

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import org.testng.Assert;
import org.testng.annotations.Test;

public class HashSet1 {

    @Test
    public void t1() {
        Set<String> set1 = new HashSet<>();
        set1.add("a");
        set1.add("b");
        Set<String> set2 = new HashSet<>();
        set2.add("a");
        for (String s : set1) {
            if (set2.contains(s)) {
                System.out.println(s);
            }
        }

        if (set1.equals(set2)) {
            System.out.println("set1 equals set2");
        }
    }

    /**
     * 两个set元素相同时调用equals比较，结果为true
     */
    @Test
    public void t2() {
        Set<String> s1 = new HashSet<>();
        s1.add("a");
        s1.add("c");
        s1.add("b");

        Set<String> s2 = new HashSet<>();
        s2.add("a");
        s2.add("b");
        s2.add("c");

        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        }

//        Assert.assertSame(s1, s2);
//        Assert.assertEquals(s1, s2);
    }

    /**
     * 两个没有元素的HashSet调用equals方法，结果为true
     */
    @Test
    public void t3() {
        var s1 = new HashSet<>();
        var s2 = new HashSet<>();
        // true
        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        }
    }

    /**
     * set remove
     */
    @Test
    public void t4() {
        Set<String> s1 = new HashSet<>();
        s1.add("a");
        s1.add("b");
        System.out.println(s1);
        s1.remove("a");
        System.out.println(s1);
    }

    /**
     * addAll方法
     */
    @Test
    public void t5() {
        var set1 = new HashSet<String>();
        var set2 = new HashSet<String>();

        set1.add("a");
        set1.add("b");
        set1.add(null);

        set2.add("1");
        set2.addAll(set1);

        System.out.println(set2);

    }

    /**
     * TreeSet传入比较器，当出现和set元素中比较项相同的元素时，不会存入set
     * 换言之，TreeSet会去重
     */
    @Test
    private void t6() {
        var set = new TreeSet<>(Comparator.comparingLong(PersonObj::getAge));

        PersonObj p1 = new PersonObj(1L, 23);
        PersonObj p2 = new PersonObj(3L, 21);
        PersonObj p3 = new PersonObj(3L, 2);
        PersonObj p4 = new PersonObj(3L, 38);
        PersonObj p5 = new PersonObj(4L, 38);

        set.add(p1);
        set.add(p2);
        set.add(p3);
        set.add(p4);
        set.add(p5);

        System.out.println(set);
    }

    @Test
    private void t7() {
        // 使用了Integer.compare方法，该方法是降序的，大数在前
        var set = new TreeSet<>(Comparator.comparingInt(PersonObj::getAge));

    }

    @Test
    private void t8() {
        var set = new HashSet<Integer>();

        var b = set.stream()
            .noneMatch(e -> e == 0);
    }



    class PersonObj {

        private Long id;


        private int age;

        public PersonObj(Long id, int age) {
            this.id = id;
            this.age = age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }


        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        @Override public String toString() {
            return this.id + "_" + this.age;
        }

        @Override public int hashCode() {
            return super.hashCode();
        }

        @Override public boolean equals(Object obj) {
            return super.equals(obj);
        }
    }

}
